﻿Option Explicit On
Option Strict On

''' <summary>
''' Formulario Modificar Cliente: Para modificar los datos de un cliente existente en la BBDD
''' </summary>
''' <author>Jose Manuel Rizo</author>

Public Class ModificarCliente

    Private miBBDD As BBDD
    Private listaCliente As List(Of Cliente)

    Private Sub ModificarCliente_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        miBBDD = New BBDD
        listaCliente = New List(Of Cliente)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ComboBox3.SelectedIndex > -1 Then
            listaCliente(ComboBox3.SelectedIndex).Nombre = TextBox2.Text
            listaCliente(ComboBox3.SelectedIndex).Apellidos = TextBox3.Text
            If (comprobarDNI(TextBox1.Text)) Then
                listaCliente(ComboBox3.SelectedIndex).DNI = TextBox1.Text
            End If
            If comprobarTelefono(TextBox4.Text) Then
                listaCliente(ComboBox3.SelectedIndex).Telefono = TextBox4.Text
            End If
            If comprobarEmail(TextBox7.Text) Then
                listaCliente(ComboBox3.SelectedIndex).Email = TextBox7.Text
            End If


            If (miBBDD.ModificaCliente(listaCliente(ComboBox3.SelectedIndex))) Then
                MsgBox("Usuario agregado con éxito", , "Completado")
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                TextBox7.Text = ""
            Else
                MsgBox("Hubo un problema al modificar usuario", , "Fallo al modificar")
            End If
        End If
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        ComboBox3.Items.Clear()
        miBBDD.Conectar()
        listaCliente = miBBDD.ListaClientes("SELECT * FROM cliente WHERE Nombre like ? or apellidos like ?", TextBox5.Text)
        For Each resultado As Cliente In listaCliente
            ComboBox3.Items.Add(resultado.Apellidos & ", " & resultado.Nombre)
        Next
        miBBDD.Desconectar()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        If listaCliente.Count > 0 And ComboBox3.Items.Count > 0 And ComboBox3.SelectedIndex > -1 Then
            TextBox2.Text = listaCliente(ComboBox3.SelectedIndex).Nombre
            TextBox3.Text = listaCliente(ComboBox3.SelectedIndex).Apellidos
            TextBox1.Text = listaCliente(ComboBox3.SelectedIndex).DNI
            TextBox4.Text = listaCliente(ComboBox3.SelectedIndex).Telefono
            TextBox7.Text = listaCliente(ComboBox3.SelectedIndex).Email
        End If
    End Sub
End Class